﻿using LitJson;
using System;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using VIP.Extension;

public class VIPFileUtil {

    /// <summary>
    /// 读取txt文本转换成字符串。注意：文件名需要带后缀。(默认需要填写全路径)
    /// </summary>
    /// <param name="pathEnum"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static string ReadAllText(string fileName, VIPPathEnum pathEnum = VIPPathEnum.NONE)
    {
        string path = VIPPath.GetPath(pathEnum);
        string str = File.ReadAllText(path + fileName, Encoding.UTF8);
        return str;
    }

    /// <summary>
    /// 将文本写入到硬盘
    /// Android下SteamingAssets路径变为只读，所以需要使用persisientDataPath路径来读写
    /// </summary>
    /// <param name="path"></param>
    public static void WriteAllText(string fileName, string contents,VIPPathEnum pathEnum = VIPPathEnum.NONE)
    {
        string path = VIPPath.GetPath(pathEnum).CreateDirIfNotExists();
        File.WriteAllText(path + fileName, contents, Encoding.UTF8);
        //StreamWriter sWriter = File.CreateText(path + fileName);
        //sWriter.Write(contents);
        //sWriter.Close();
    }

    /// <summary>
    /// 读取StreamingAssets文件夹下的图片。注意：文件名需要带后缀。(默认需要填写全路径)
    /// </summary>
    /// <param name="pathEnum">路径枚举</param>
    /// <param name="fileName">图片名字</param>
    /// <returns></returns>
    public static byte[] ReadImageByte(string fileName, VIPPathEnum pathEnum = VIPPathEnum.NONE)
    {
        string path = VIPPath.GetPath(pathEnum);
        //将图片以文件流的形式进行保存
        FileStream fs = new FileStream(path + fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        //将流读入到字节数组中
        byte[] bytes = br.ReadBytes((int)fs.Length);
        return bytes;
    }

    /// <summary>
    /// 将文本中的字符串解析成JsonData。注意：文件名需要带后缀。(默认需要填写全路径)
    /// </summary>
    /// <param name="fileName">文件名字</param>
    /// <param name="pathEnum">路径枚举</param>
    /// <returns></returns>
    public static JsonData ReadJsonData(string fileName, VIPPathEnum pathEnum = VIPPathEnum.NONE)
    {
        //获取到文本里面的json字符串
        string jsonStr = ReadAllText(fileName, pathEnum);
        JsonData jData = JsonMapper.ToObject(jsonStr);
        return jData;
    }

    /// <summary>
    /// 将文本中的字符串解析成JsonData并反序列化，传过来的类的属性必须与Key一致且是public的。注意：文件名需要带后缀。(默认需要填写全路径)
    /// </summary>
    /// <param name="fileName">文件名字</param>
    /// <param name="pathEnum">路径枚举</param>
    /// <returns></returns>
    public static T ReadObject<T>(string fileName, VIPPathEnum pathEnum = VIPPathEnum.NONE)
    {
        //获取到文本里面的json字符串
        string jsonStr = ReadAllText(fileName, pathEnum);
        T obj = JsonMapper.ToObject<T>(jsonStr);
        return obj;
    }

    /// <summary>
    /// 将文本复制到粘贴板
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static bool Copy(string text) {
        TextEditor textEditor = new TextEditor();
        textEditor.text = text;
        textEditor.OnFocus();
        textEditor.Copy();
        return true;
    }

}
